public class Leetcode2287 {
    public int rearrangeCharacters(String s, String target) {
        char[] sourceChars = s.toCharArray();
        char[] targetChars = target.toCharArray();
        int[] count = new int[26];
        for (char sourceChar : sourceChars) {
            count[sourceChar - 'a']++;
        }

        int[] targetCount = new int[26];
        for (char targetChar : targetChars) {
            targetCount[targetChar - 'a']++;
        }

        int res = Integer.MAX_VALUE;
        for (int i = 0; i < targetCount.length; i++) {
            if (targetCount[i] == 0) {
                continue;
            }

            res = Math.min(count[i] / targetCount[i], res);
        }


        return res;
    }
}
